package com.alog.javaee.day41;


import com.alog.common.utils.JdbcUtil;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserDaoImpl implements UserDao {

    private JdbcTemplate template = new JdbcTemplate(JdbcUtil.getDataSource());

    @Override
    public List<User> findAll() {
        //使用JDBC操作数据库...
        //1.定义sql
        String sql = "select * from user";
        List<User> users = template.query(sql, new BeanPropertyRowMapper<User>(User.class));

        return users;
    }

    @Override
    public User login(User user) {
        try {
            String sql = "select * from user where username = ? and password = ?";
            return template.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class), new Object[]{user.getUsername(), user.getPassword()});
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void add(User user) {
        String sql = "insert into user (name, gender, age, address, qq, email) values (?, ?, ?, ?, ?, ?)";
        template.update(sql, new Object[]{user.getName(), user.getGender(), user.getAge(), user.getAddress(), user.getQq(), user.getEmail()});
    }

    @Override
    public void delete(Integer id) {
        String sql = "delete from user where id = ?";
        template.update(sql, id);
    }

    @Override
    public User findById(Integer id) {
        String sql = "select * from user where id = ?";
        User user = template.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), id);
        return user;
    }

    @Override
    public void update(User user) {
        String sql = "update user set gender = ?, age = ? , address = ?, qq = ?, email = ? where id = ?";
        template.update(sql, new Object[]{user.getGender(), user.getAge(), user.getAddress(), user.getQq(), user.getEmail(), user.getId()});
    }

    @Override
    public int findTotalCount(Map<String, String[]> condition) {
        String sql = "select count(*) from user where 1 = 1 ";
        StringBuilder sb = new StringBuilder(sql);
//        String name = condition.get("name")[0];
//        if(StringUtils.isEmpty(name)){
//            sb.append("and name like %").append(name).append("% ");
//        }
//        String address = condition.get("address")[0];
//        if(StringUtils.isEmpty(address)){
//            sb.append("and address like %").append(address).append("% ");
//        }
//        String email = condition.get("email")[0];
//        if(StringUtils.isEmpty(email)){
//            sb.append("and email like %").append(email).append("% ");
//        }
        List<Object> params = new ArrayList<>();
        for( Map.Entry<String, String[]> en : condition.entrySet()){
            if("currentPage".equals(en.getKey())){
                continue;
            }
            String value = en.getValue()[0];
            if(!StringUtils.isEmpty(value)){
                sb.append("and ").append(en.getKey()).append(" like ? ");
//                params.add("%" + value + "%");
            }
        }
        System.out.println(sb.toString());
        if(params.size() > 0){
            return template.queryForObject(sb.toString(), Integer.class, params);
        } else {
            return template.queryForObject(sql, Integer.class);
        }

    }

    @Override
    public List<User> findByPage(int start, int pageSize, Map<String, String[]> condition) {
        String sql = "select * from user limit ?, ?";
        List<Object> params = new ArrayList<>();
        StringBuilder sb = new StringBuilder(sql);
//        for( Map.Entry<String, String[]> en : condition.entrySet()){
//            if("currentPage".equals(en.getKey())){
//                continue;
//            }
//            String value = en.getValue()[0];
//            if(!StringUtils.isEmpty(value)){
//                sb.append("and ").append(en.getKey()).append(" like ? ");
//                params.add(value);
//            }
//        }
//        sb.append("limit ?, ?");
//        params.add(start);
//        params.add(pageSize);
//        System.out.println(sb.toString());
        return template.query(sb.toString(), new BeanPropertyRowMapper<>(User.class), start, pageSize);
    }
}
